from lxml import etree
data="""
<div>
    <ul>
        <li class="item-0"><a href="link1.html">first item</a></li>
        <li class="item-1"><a href="link2.html">second item</a></li>
        <li class="item-inactive"><a href="link3.html"><span class="bold">third item</span></a></li>
        <li class="item-1"><a href="link4.html">fourth item</a></li>
        <li class="item-0"><a href="link5.html">fifth item</a></li>
    </ul>
</div>
"""
html=etree.HTML(data)
text=etree.tostring(html).decode()
print(text)
query1=html.xpath('//li')
query2=html.xpath('//li/@class')
query3=html.xpath('//li/a[@href="link1.html"]')
query4=html.xpath('//li[1]/a')
query5=html.xpath('//li//span')
query6=html.xpath('//li[last()]/a/@href')
query7=html.xpath('/html/body/div/ul/li/a/text()')
query8=html.xpath('//a[contains(@href,"link")]')
query9=html.xpath('//div//li[last()-1]')
for i in range(1,10):
    search='query'+str(i)
    if isinstance(eval(search)[0],str):
        print(eval(search))
    else:
        text=etree.tostring(eval(search)[0]).decode()
        print(text)
query10=html.xpath('//div//li[position()>1]')
for i in range(len(query10)):
    text=etree.tostring(query10[i]).decode()
    print(text)
query11=html.xpath('//div/a|//div/ul')
query12=html.xpath('//div//li[start-with(@class,"item")]')
query13=html.xpath('//div//a[ends-with(@href,"html")]')
print(query11,query12,query13)
